<?php

namespace App\models;

use Illuminate\Database\Eloquent\Model;
use App\Models\Member;
use App\Models\LuckLogs;
use App\Models\LuckSet;

class Luck extends Model{

    const CREATED_AT = 'create_time'; // 创建时间
    const UPDATED_AT = 'update_time'; // 开始时间

    protected $table="luck"; // 数据库表
    protected $dateFormat = 'U'; // 时间戳

    /**
     * 幸运值榜单
     * @return [type] [description]
     */
    public static function rankingList() {
        $resultList = [];
        //获取排行榜
        foreach (self::select(["openid","value"])->orderBy("value","desc")->take(20)->get() as $item) {
            $resultList[]  = $item->toArray();
        }

        $openIds = array_column($resultList,"openid");
        //获取会员信息
        $memberList = Member::listMember($openIds,"openid");
        //整合信息
        foreach ($resultList as &$item) {
            if (isset($memberList[$item['openid']])){
                $item['nickName'] = $memberList[$item['openid']]['username'];
                $item['avatarUrl'] = $memberList[$item['openid']]['head_ico'];
            }
        }
        return $resultList;
    }


    /**
     * 修改幸运值
     *
     * @author RJ
     * @time 2018.02.03
     * @param  string  $openid 用户openid
     * @param  int  $state 状态 1:签到 2:发红包 3:领红包
     * @param  int  $type 1:增加幸运值 2:减少幸运值
     * @param  bool $is_check 是否检测
     * @return array
     */
    public static function updateLuckValue($openid = '', $state = 1, $type = 1, $is_check = false){

        if(!empty($openid)){
            $luck = new Luck(); // 实例化对象
            // 搜索openid获取用户幸运值信息
            $luck_info = $luck
                ->where(function($query)use($openid){
                    $query -> where('openid',$openid);
                })
                ->first();

            // 获取幸运值
            if(!empty($state)){
                $LuckLogs = new LuckLogs();
                if($is_check == true){
                    $check_log_info = $LuckLogs->checkLuckLog($openid,$state); // 判断是否超过任务数量
                    if($check_log_info == false){
                        return '领取次数已经超过上限';
                    }
                }
                $LuckSet = new LuckSet(); // 实例化对象
                $score_info = $LuckSet->getLuckScore($state); // 获取幸运值信息
                $score = $score_info['score']; // 幸运值
                $name = $score_info['name']; // 类型名称

                // 判断是否存在幸运值信息
                if(!empty($luck_info)){
                    // 存在
                    if($type == 1){
                        // 增加积分
                        $luck->where('openid',$openid)->increment('value',$score);
                    }else if($type == 2){
                        // 减少积分
                        $luck->where('openid',$openid)->decrement('value',$score);
                    }
                }else{
                    // 不存在 新增一条记录
                    $luck->openid = $openid;
                    $luck->value = $score;
                    $luck->save();
                }
                $LuckLogs->addLuckLog($openid,$score,$name,$state,$type); // 添加幸运值记录
                $data = array(
                    'code' => 200,
                    'result' => array(
                        'score' => $score,
                    ),
                );
                return $data;
            }else{
                return '没有获取到幸运值';
            }
        }else{
            return 'openid不存在';
        }
    }

    public static function getLuck($openid = ''){
        $Luck = new self;
        if(!empty($openid)){
            $where = array(
                'openid' => $openid
            );
            $take = 20;
        }
        $data = $Luck
            ->select(["openid","value"])
            ->where($where)
            ->take($take)
            ->get();
        $data = $data->toArray();
        return $data;
    }
}
